﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace AndroidReverseExploitShell
{
    /// <summary>
    /// provides a wrapper class that can be used to display a web browser
    /// </summary>
    public partial class ARESBrowser : Form
    {
        private string cookieData = "";
        /// <summary>
        /// creates a new browser instance and navigates to the given url
        /// </summary>
        /// <param name="url">the url of the website to display</param>
        /// <param name="cookies">the cookie data to use</param>
        private ARESBrowser(string url, string cookies)
        {
            InitializeComponent();
            cookieData = cookies;
            webBrowser.Navigate(url);
        }
        /// <summary>
        /// creates a new browser instance and displays a given html code
        /// </summary>
        /// <param name="html">the html code to display</param>
        private ARESBrowser(string html)
        {
            InitializeComponent();
            webBrowser.DocumentText =  "<html>"+html+"</html>";
        }
        /// <summary>
        /// creates a new browser instance and navigates to the given url
        /// </summary>
        /// <param name="url">the url of the website to display</param>
        /// <param name="cookies">the cookie data to use</param>
        public static void ShowURL(string url, string cookies)
        {
            string[] paramArray = new string[2];
            paramArray[0] = url;
            paramArray[1] = cookies;

            ParameterizedThreadStart threadStart = new ParameterizedThreadStart(CreateBrowserAndShowIt);
            Thread thread = new Thread(threadStart);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(paramArray);
        }
        /// <summary>
        /// creates a new browser instance and displays a given html code
        /// </summary>
        /// <param name="html">the html code to display</param>
        public static void ShowHTML(string html)
        {
            ParameterizedThreadStart threadStart = new ParameterizedThreadStart(CreateBrowserAndShowIt);
            Thread thread = new Thread(threadStart);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start(html);
        }
        /// <summary>
        /// creates a new browser instance and shows it to the user
        /// </summary>
        /// <param name="target">the parameters to pass to the browser</param>
        private static void CreateBrowserAndShowIt(object target)
        {
            ARESBrowser browser;
            if (target is string)
            {
                browser = new ARESBrowser(target as string);
            }
            else
            {
                string[] paramArray = target as string[];
                browser = new ARESBrowser(paramArray[0], paramArray[1]);
            }
            Application.Run(browser);
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!cookieData.Equals(""))
            {
                webBrowser.Document.Cookie = cookieData;
                cookieData = "";
                webBrowser.Refresh();
            }
        }
    }
}
